package com.mkx.configuration;

import com.mkx.core.ml.RedisLockMLPredictor;
import com.mkx.properties.RedisLockProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * Redis分布式锁机器学习配置类
 * 用于初始化和配置机器学习预测器
 */
@Component
@ConditionalOnProperty(prefix = "redis.lock", value = "enabled", havingValue = "true", matchIfMissing = true)
public class RedisLockMLConfiguration implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(RedisLockMLConfiguration.class);
    
    private final RedisLockProperties properties;
    
    public RedisLockMLConfiguration(RedisLockProperties properties) {
        this.properties = properties;
    }
    
    @Override
    public void afterPropertiesSet() {
        try {
            // 初始化机器学习预测器
            RedisLockMLPredictor predictor = RedisLockMLPredictor.getInstance();
            
            // 设置配置参数
            predictor.setEnabled(properties.isMlPredictionEnabled());
            predictor.setHistoryWindowSize(properties.getMlHistoryWindowSize());
            predictor.setAlpha(properties.getMlExponentialSmoothingAlpha());
            predictor.setSeasonalWeight(properties.getMlSeasonalWeight());
            
            logger.info("RedisLockMLPredictor initialized with config: enabled={}, historyWindowSize={}, alpha={}, seasonalWeight={}",
                    properties.isMlPredictionEnabled(),
                    properties.getMlHistoryWindowSize(),
                    properties.getMlExponentialSmoothingAlpha(),
                    properties.getMlSeasonalWeight());
        } catch (Exception e) {
            logger.error("Failed to initialize RedisLockMLPredictor", e);
        }
    }
}